<?php

namespace App\Http\Controllers\Wechat;

use App\Libraries\classs\Face;
use App\Libraries\classs\Upload;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Redirect, Input, Response;
use App\Models\Buildings;
use App\Models\User\Vote\Logs as Logs;
class UploadController extends BaseController
{
    public function index($id){
        if(empty($this->_user->phone) && empty($this->_user->username)){
            return redirect('/wechat/mine/info/'.$id);
        }
        $data['build']=Buildings::find($id);
      return view('wechat.upload.index',$data);
    }

    public function create(){

    }

    public function uploadImg(Request $request){
        $base64=$request->get('base64');
        $build_id =$request->get('build_id');
        if($base64){
           if(Face::getInstance()->getFaceCount($this->_uid) >= 3) {
               return Response::json(array('success'=>false,'msg'=>"你今天的3次投票机会已用完，明天再来投票哦"));
           }
           $upload_rs=Upload::getInstance()->upload_base64($base64);
            if($upload_rs['success']) {
                $face = Face::getInstance()->createFace($this->_uid, $upload_rs['id'], 1);
                if ($face) {
                    $build = Buildings::find($build_id);
                    $user_vote_data = array(
                        'x_uid' => $this->_uid,
                        'build_id' => $build_id,
                        'count' => $face->star,
                    );
                    if (Logs::create($user_vote_data)) {
                        $build->vote = $face->star+$build->vote;
                        $build->save();
                    }
                    return Response::json(array('success' => true, 'url' => $upload_rs['url'], 'face' => $face, 'build' => $build));
                }
            }
        }
        return Response::json(array('success'=>false,'msg'=>"不是人脸头像,请重新上传"));
    }
}
